<template>
  <div>
    <el-row>
      <el-col :span="24">
        <el-card style="width: 100%;height:70px">
          <center>
            <a style="font-size: large;">
              <b>分类管理</b>
            </a>
          </center>
        </el-card>
      </el-col>
    </el-row>
    <br />
    <el-row>
      <el-col :span="24">
        <el-card style="height:500px;overflow-y: auto;">
          <el-table
            :data="tableData"
            style="width: 100%"
            row-key="id"
            border
            @selection-change="handleSelectionChange"
            lazy
            :load="load"
            :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
          >
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column prop="typeName" label="类型名称"></el-table-column>
            <el-table-column prop="status" label="状态" :formatter="statusFormatter">
              <template slot-scope="scope">
                <span :class="getStatusClass(scope.row.status)">{{ getStateText(scope.row.status) }}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <a
                  @click="addNewType(scope.row)"
                  style="color:dodgerblue;"
                  v-if="scope.row.parentId==-1"
                >添加</a>&nbsp;
                <!-- <a @click="deleteType(scope.row)" style="color:dodgerblue;">删除</a>&nbsp; -->
                <a @click="updateType(scope.row)" style="color:dodgerblue;">修改</a>
                &nbsp;
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
    </el-row>

    <el-dialog
      title="子菜单"
      :visible.sync="centerDialogVisible"
      width="30%"
      center
      :destroy-on-close="true"
    >
      <center>
        子菜单名称：
        <el-input placeholder="请输入" v-model="addNewMnnuName" clearable style="width: 200px;"></el-input>
      </center>
      <br />
      <center>
        子菜单状态：
        <el-select v-model="addStatus" clearable placeholder="请选择" style="width: 200px;">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </center>
      <br />

      <center>
        <el-button size="mini" type="primary" @click="addNewChild">{{ buttonType }}</el-button>
      </center>
    </el-dialog>
  </div>
</template>
  
<script>
export default {
  data() {
    return {
      options: [
        {
          value: 1,
          label: "启用"
        },
        {
          value: 2,
          label: "禁用"
        }
      ],
      tableData: "",
      id: "",
      centerDialogVisible: false,
      addStatus: "",
      addNewMnnuName: "",
      parentId: "",
      buttonType: ""
    };
  },
  methods: {
    addNewChild() {
      if (this.buttonType == "修改") {
        let param = {};
        param.typeName = this.addNewMnnuName;
        param.status = this.addStatus;
        param.id = this.id;
        this.$axios.post("api/scss-server/updateType",param).then(res => {
          if (res.data.code == 200) {
            this.parentId = "";
            this.centerDialogVisible = false;
            this.$message({
              type: "success",
              message: "修改成功！"
            });
            this.queryAllProductType(); // 刷新数据
          }
        });
        return;
      }
      let param = {};
      param.parentId = this.parentId;
      param.typeName = this.addNewMnnuName;
      param.status = this.addStatus;
      this.$axios.post("api/scss-server/addNewChild", param).then(res => {
        if (res.data.code == 200) {
          this.parentId = "";
          this.$message({
            type: "success",
            message: "添加成功！"
          });
          this.centerDialogVisible = false;
          this.queryAllProductType(); // 刷新数据
        }
      });
    },
    addNewType(val) {
      this.buttonType = "添加";
      this.centerDialogVisible = true;
      this.id = val.id;
    },
    deleteType(val) {
      // 实现删除逻辑
    },
    updateType(val) {
      this.buttonType = "修改";
      // 实现更新逻辑
      this.addNewMnnuName = val.typeName;
      this.addStatus = val.status;
      this.id = val.id;
      this.centerDialogVisible = true;
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    queryAllProductType() {
      this.$axios.get("api/scss-server/queryAllProductType").then(res => {
        if (res.data.code == 200) {
          this.tableData = res.data.data;
        }
      });
    },
    getStateText(status) {
      // console.log(status)
      switch (status) {
        case 2:
          return "禁用";
        case 1:
          return "启用";
        default:
          return "";
      }
    },
    getStatusClass(status) {
      switch (status) {
        case 2:
          return "status-disabled";
        case 1:
          return "status-enabled";
        default:
          return "";
      }
    }
  },
  created() {
    this.queryAllProductType();
  }
};
</script>
  
<style scoped>
.status-enabled {
  color: green;
}
.status-disabled {
  color: red;
}
</style>